﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="NewProjectViewModel.cs" company="Mar3ek">
//   http://mar3ek.wordpress.com
// </copyright>
// <summary>
//   Defines the NewProjectViewModel type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Mar3ek.ColorHatTimeTracker
{
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;

    using ColorhatNet;

    /// <summary>
    /// The new project view model.
    /// </summary>
    public class NewProjectViewModel : INotifyPropertyChanged
    {
        /// <summary>
        /// The name of the project to create
        /// </summary>
        private string projectName;

        /// <summary>
        /// Stores the selected customer
        /// </summary>
        private string selectedCustomer;

        /// <summary>
        /// The property changed event handler delegate.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Gets the ColorHat API
        /// </summary>
        public ColorhatApi Api { get; private set; }

        /// <summary>
        /// Gets the customers.
        /// </summary>
        public IEnumerable<string> Customers
        {
            get
            {
                return this.Api == null ? null : this.Api.Customers.Values;
            }
        }

        /// <summary>
        /// Gets or sets the project name.
        /// </summary>
        public string ProjectName
        {
            get
            {
                return this.projectName;
            }

            set
            {
                this.projectName = value;
                this.OnPropertyChanged();
            }
        }

        /// <summary>
        /// Gets or sets the selected customer.
        /// </summary>
        public string SelectedCustomer
        {
            get
            {
                return this.selectedCustomer;
            }

            set
            {
                this.selectedCustomer = value;
                this.OnPropertyChanged();
            }
        }

        /// <summary>
        /// Initializes this view model.
        /// </summary>
        /// <param name="api">
        /// The ColorHat API implementation.
        /// </param>
        /// <param name="newProjectName">
        /// The project name.
        /// </param>
        public void Initialize(ColorhatApi api, string newProjectName)
        {
            this.Api = api;
// ReSharper disable ExplicitCallerInfoArgument
            this.OnPropertyChanged("Customers");
// ReSharper restore ExplicitCallerInfoArgument
            this.ProjectName = newProjectName;
        }

        /// <summary>
        /// The on property changed event invocator.
        /// </summary>
        /// <param name="propertyName">
        /// The property name.
        /// </param>
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
